package com.hotfire.template.common;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

public class ApplicationContextHolder extends ContextLoaderListener {
	private static WebApplicationContext attribute;


    @Override
    public void contextInitialized ( ServletContextEvent sce )
    {
        super.contextInitialized(sce);
        attribute = (WebApplicationContext) sce.getServletContext().
                getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);
    }
    
	/**
	 * 获取spring bean
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		if(attribute != null)
			return attribute.getBean(beanName);
		return null;
	}

}
